package com.zhang.cicadatravels.main.travels;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.youth.banner.Banner;
import com.zhang.cicadatravels.R;
import com.zhang.cicadatravels.base.BaseFragment;
import com.zhang.cicadatravels.utils.SingleQueue;
import com.zhang.cicadatravels.utils.Values;

import java.lang.reflect.Type;
import java.util.ArrayList;

/**
 * Created by dllo on 16/6/17.
 */
public class TravelsFragment extends BaseFragment implements OnRefreshListener {
    private RefreshListView listView;
    private ArrayList<TravelsFragmentListViewBean> datas;
    private TravelsFragmentAdapter adapter;
    private Banner banner;


    @Override
    public int initlayout() {
        return R.layout.fragment_travels;
    }

    @Override
    public void initview(View view) {
        listView = (RefreshListView) view.findViewById(R.id.fragment_travels_listview);
        datas = new ArrayList<>();
        adapter = new TravelsFragmentAdapter(context);
        View view1 = LayoutInflater.from(context).inflate(R.layout.travels_banner, null);
        banner = (Banner) view1.findViewById(R.id.activity_main_banner);
        listView.addHeaderView(view1);

    }

    @Override
    public void initdata() {

        getChangeView();
        getListViewData();
        listView.setAdapter(adapter);

    }

    private void getChangeView() {
        RequestQueue requestQueue = SingleQueue.getSingleQueue(context).getQueue();
        StringRequest stringRequest = new StringRequest(Values.MAIN_ACTIVITY_BANNER_URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d("lanou", response);
                Type type = new TypeToken<ArrayList<TravelsFragmentRingImageBean>>() {

                }.getType();
                Gson gson = new Gson();
                ArrayList<TravelsFragmentRingImageBean> bean = gson.fromJson(response, type);
                String[] urls = new String[bean.size()];
                for (int i = 0; i < bean.size(); i++) {
                    urls[i] = bean.get(i).getImage_url();
                }
                banner.setDelayTime(3000);
                banner.setImages(urls);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        requestQueue.add(stringRequest);


    }

    private void getListViewData() {
        RequestQueue requestQueue = SingleQueue.getSingleQueue(getContext()).getQueue();
        StringRequest stringRequest = new StringRequest(Values.TRAVELS_FRAGMENT_LISTVIEW_URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                Type type = new TypeToken<ArrayList<TravelsFragmentListViewBean>>() {
                }.getType();
                datas = gson.fromJson(response, type);
                Log.d("xxxxxx", datas.get(0).getDays() + "");
                adapter.setBeans(datas);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        requestQueue.add(stringRequest);
    }


    @Override
    public void onDownPullRefresh() {
        Toast.makeText(context, "刷新", Toast.LENGTH_SHORT).show();
        adapter.notifyDataSetChanged();

    }

    @Override
    public void onLoadingMore() {
        Toast.makeText(context, "加载", Toast.LENGTH_SHORT).show();

        adapter.notifyDataSetChanged();
    }
}
